<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 品牌ajax类
 * @package  	ajax
 * @subpackage  backend
 * @author   	allen.qiang
 */

quanli_require('module/base/goodsHandler.class.php');
quanli_require('module/base/fileDirHandler.class.php');


class ajax_brand {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询商品品牌
                    $this->searchBrand();
                    break;

			    case 2: //添加或修改商品品牌
                    $this->editBrand();
                    break;

				case 4: //删除商品品牌
					$this->deleteBrand();
					break;

				case 5: //商品品牌使用标志 
					$this->actionBrand();
					break;

                case 6: //待编辑商品品牌查询
                    $this->searchOneBrand();
                    break;

                case 7: //查询商品品牌最大序列号
                    $this->searchSort();
                    break;

                default:
                    $this->toReturn('参数错误');
                    break;

            }

        } else {

            $this->toReturn('参数错误');

        }

    }

    /**
     * 返回信息
     * @param string $message
     * @param int $code
     * @param mixed $data
     */
    private function toReturn( $message, $code = 400, $data = '' ) {

        die( json_encode( array( 'code' => $code, 'message' => $message, 'data' => $data ) ) );

    }

    /**
     * 查询商品品牌
     */
    private function searchBrand() {

        global $G_PGLOB;

        //实例商品类
        $goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;

        //查询品牌信息
        $arg_array = $_REQUEST;
        $brand_array = $goodsHandler->searchGoodsBrand( $arg_array );
        
		//查询结果
		$result_array = array(
            'page'  => $arg_array['page'] ,
            'total' => $arg_array['count'] ,
            'rows'  => array()
        );
        
		if ($brand_array) {

			$edit_permission = $G_PGLOB->has_permission(503);

            foreach($brand_array as &$d_l) {

            	if($edit_permission){
	                $result_array['rows'][] = array(
	                    'id' 	=> $d_l['brandID'],
	                    'cell' 	=> array(
		        			'<a href="javascript: void(0);" onclick="brand.edit(' . $d_l['brandID'] . ')">修改</a>&nbsp;' ,
		        			$d_l['brandName'],
		       				$d_l['description']	,
		       				$d_l['sortOrder'],
		        			'<a href="javascript: void(0);" onclick="brand.change.check(' . $d_l['brandID'] . ', ' . $d_l['useFlg'] . ') " >' . $d_l['useName'] . '</a>'
		        		),
	                );
            	}else{
            		$result_array['rows'][] = array(
	                    'id' 	=> $d_l['brandID'],
	                    'cell' 	=> array(
	            			$d_l['brandName'],
	           				$d_l['description']	,
	           				$d_l['sortOrder'],
	            			$d_l['useName']
	            		),
	                );
            	}

            }

        }

        echo json_encode( $result_array );
    }

    
    /**
     * 新增或修改商品品牌信息
     */
	private function editBrand(){

		global $G_PGLOB;

    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;

        $_REQUEST['operator'] = $G_PGLOB->currentUserID;
    	$result_array = $goodsHandler->editBrandInfo( $_REQUEST ) ;

    	echo json_encode($result_array);

	}
	/**
	 * 删除品牌信息
	 *
	 */
	private function deleteBrand () {

		global $G_PGLOB;
		
    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;

    	$result_array = $goodsHandler->deleteBrand( $_REQUEST ) ;

    	echo json_encode($result_array);

	}
	/**
	 * 更新品牌使用状态 
	 *
	 */
	private function actionBrand () {

		global $G_PGLOB;
		
    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;

    	$result_array = $goodsHandler->changeBrandUse( $_REQUEST ) ;

    	echo json_encode($result_array);

	}

    /**
     * 待编辑品牌查询
     *
     */
    private function searchOneBrand () {

        global $G_PGLOB;

        //实例商品类
        $goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;

        //查询品牌信息
        $arg_array = array('brandID'=>$_REQUEST['brandID']);
        $brand_array = $goodsHandler->searchGoodsBrand( $arg_array );
        $brand_img= explode("?",$brand_array[0]['logo']);
        $brand_array[0]['brand_img']=$brand_img[0];
        echo json_encode($brand_array[0]);

    }

    //查询最大序列号
    private function searchSort(){

        global $G_PGLOB;

        //实例化公共类
        $commHandler = new commHandler( $G_PGLOB->DBCA );

        //获得最大的排序序号
        $sort_num = $commHandler->getSortNum('goods_brand');
        if ($sort_num == 0) {
            $this->toError('参数错误');
        }else{
            $result = array('sortOrder'=>$sort_num);
        }
        echo json_encode($result);
    }

}
